package hvzt.minimap.asynctasks;

import hvzt.minimap.activities.ServerActivity;
import iface.Envelope;
import iface.MessageInterface;
import iface.Registration;
import android.os.AsyncTask;

/**
 * RegistrationTask sends a Registration message to the server, calling methods on the UI thread to update
 * progress and handle the result from the server.
 * @author Franklin Pearsall
 *
 */
public class RegistrationTask extends AsyncTask<Registration, Progress, iface.Status>{
	
	private ServerActivity m_s;
	
	public RegistrationTask(ServerActivity s) {
		m_s = s;
	}
	
	/**
	 * Sends a registration message to the server and returns the response.
	 */
	@Override
	protected iface.Status doInBackground(Registration... params) {
		publishProgress(Progress.IN_PROGRESS);
		Registration r = params[0];
	    Envelope packet = new Envelope(r.getUser(), r);
	    iface.Status status = MessageInterface.sendEnvelopeToServer(packet);
	    publishProgress(Progress.DONE);
	    return status;
	}

	/**
	 * Hands the server response to the activity.
	 */
	@Override
	protected void onPostExecute(iface.Status result) {
		m_s.handleResult(result);
		super.onPostExecute(result);
	}

	/**
	 * Updates the progress on the UI.
	 */
	@Override
	protected void onProgressUpdate(Progress... values) {
		Progress p = values[0];
		m_s.updateProgress(p);
		super.onProgressUpdate(values);
	}

}
